package com.copa.module.cart.controller;

import com.copa.domain.ResponseDto;
import com.copa.module.cart.domain.dto.CartListDto;
import com.copa.module.cart.domain.vo.CartAddVo;
import com.copa.module.cart.domain.vo.CartUpdateVo;
import com.copa.module.cart.service.CartService;
import com.copa.utils.WechatUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

/**
 * @author copa
 * @createDate 2021-01-18 15:13
 * @function
 */
@RestController
@Api(tags = {"购物车接口"})
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/cart/list")
    @ApiOperation(value = "购物车列表")
    public ResponseDto<List<CartListDto>> list(@AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return cartService.list(openId);
    }

    @PostMapping("/cart/put")
    @ApiOperation(value = "修改购物车数量")
    public ResponseDto<String> put(@RequestBody CartUpdateVo cartUpdateVo,
                                   @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return cartService.put(openId, cartUpdateVo);
    }

    @GetMapping("/cart/delete")
    @ApiOperation(value = "删除购物车记录")
    public ResponseDto<String> delete(Long cartId,
                                   @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return cartService.delete(openId, cartId);
    }

    @PostMapping("/cart/post")
    @ApiOperation(value = "增加购物车记录:有记录先修改，无则添加")
    public ResponseDto<String> post(@RequestBody CartAddVo cartAddVo,
                                      @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return cartService.post(openId, cartAddVo);
    }

}
